package cn.itheima.taotao.service.impl;

import cn.itheima.taotao.domain.*;
import cn.itheima.taotao.mapper.ItemDescMapper;
import cn.itheima.taotao.mapper.ItemMapper;
import cn.itheima.taotao.service.ItemService;
import cn.itheima.taotao.utils.IDUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator .
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Resource
    private ItemMapper itemMapper;

    @Resource
    private ItemDescMapper itemDescMapper;

    @Override
    public DataGridResult getItemList(Integer page, Integer rows) {
        ItemExample itemExample = new ItemExample();
        PageHelper.startPage(page, rows);
        List<Item> items = itemMapper.selectByExample(itemExample);
        DataGridResult dataGridResult = new DataGridResult();
        dataGridResult.setRows(items);
        PageInfo pageInfo = new PageInfo(items);
        dataGridResult.setTotal(pageInfo.getTotal());
        return dataGridResult;
    }

    @Override
    public TaotaoResult saveTtem(Item item, String desc) {
        Date date = new Date();
        item.setId(IDUtils.genItemId());
        item.setStatus((byte) 1);
        item.setCreated(date);
        item.setUpdated(date);
        itemMapper.insert(item);
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(item.getId());
        itemDesc.setItemDesc(desc);
        itemDesc.setCreated(date);
        itemDesc.setUpdated(date);
        itemDescMapper.insert(itemDesc);
        return TaotaoResult.ok();
    }
}
